#pragma once

#include <cstdint>
#include <cstdlib>

template<typename T, size_t buffer_size = 100>
class sfif_RingBuffer{
 private:
  int start;    /* index of oldest element              */
  int end;      /* index at which to write new element  */
  T   *buffer;  /* vector of elements                   */

 public:
  sfif_RingBuffer() {
    start  = 0;
    end    = 0;
    buffer = new T[buffer_size];
  }
 
  ~sfif_RingBuffer() {
    delete[] buffer; /* OK if null */ 
  }
  
  bool full() {
    return (end + 1) % buffer_size == start; 
  }
 
  bool empty() {
    return end == start; 
  }

/* Write an element, overwriting oldest element if buffer is full. App can
   choose to avoid the overwrite by checking cbIsFull(). */
  void write(T *value) {
    buffer[end] = *value;
    end = (end + 1) % buffer_size;
    if (end == start)
      start = (start + 1) % buffer_size; /* full, overwrite */
  }
 
  /* Read oldest element. App must ensure !cbIsEmpty() first. */
  void read(T *value) {
    *value = buffer[start];
    start = (start + 1) % buffer_size;
  }

  size_t getSize(){
    return buffer_size;
  }
 
};

